<?php

namespace App\Admin\Controllers;

use App\Models\Channel;
use Encore\Admin\Admin;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Show;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Tree;

class ChannelsController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '多级分类';

    public function index(Content $content)
    {
        return new Content(function (Content $content) {
            $content->header('产品分类');
            #$content->body(Categories::tree());
            $content->row(function (Row $row) use ($content) {
                $row->column(6,Channel::tree());
                $row->column(6,$this->form());
            });
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Channel());

        $grid->column('id', __('Id'));
        $grid->column('pid', __('Pid'));
        $grid->column('title', __('Title'));
        $grid->column('order', __('Order'));
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Channel::findOrFail($id));

        //$show->field('id', __('Id'));
        $show->field('pid', __('Pid'));
        $show->field('title', __('Title'));
        $show->field('order', __('Order'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {

        $form= new Form(new Channel(), function (Form $form) {

            //$form->display('id', 'ID');

            $form->select('pid', '所属类别')->options(
                Channel::selectOptions()
            );

            $form->text('title', '类别名称')->rules('required');

            $form->setAction(route('channel.store'));
            //$form->display('created_at', 'Created At');
            //$form->display('updated_at', 'Updated At');
        });

        //return Admin::form(Categories::class, );

        return $form;
    }
}
